
goog.provide('esn.StrainedNetworkSurvey');

goog.require('esn.BaseSurvey');
goog.require('esn.FacebookManager');
goog.require('esn.StrainedFriendSurveyList');
goog.require('esn.constants');
goog.require('esn.model.StrainedFriendSurveyModel');
goog.require('esn.model.StudentModel');
goog.require('esn.soy.strainedNetworkSurvey');
goog.require('goog.array');
goog.require('goog.dom');
goog.require('goog.style');
goog.require('goog.ui.AutoComplete.Basic');
goog.require('goog.ui.CustomButton');
goog.require('goog.ui.LabelInput');


/**
 * @constructor
 */
esn.StrainedNetworkSurvey = function(appContext, students, opt_domHelper) {
  goog.base(this, appContext, esn.StrainedNetworkSurvey.SECTION_ID,
      opt_domHelper);

  this.appContext_ = appContext;

  this.surveyClient_ = this.appContext_.getService(
      esn.constants.ServiceId.SURVEY_CLIENT);

  this.students_ = students;

  this.fbMgr_ = new esn.FacebookManager(this.appContext_, this.students_);
};
goog.inherits(esn.StrainedNetworkSurvey, esn.BaseSurvey);

esn.StrainedNetworkSurvey.SECTION_ID = 'strained_network';

esn.StrainedNetworkSurvey.EventType = {
  BACK: goog.events.getUniqueId('back'),
  CONTINUE: goog.events.getUniqueId('continue')
};

esn.StrainedNetworkSurvey.prototype.autocomplete_;

esn.StrainedNetworkSurvey.prototype.strainedFriendSurveryList_;

esn.StrainedNetworkSurvey.prototype.continueButton_;

esn.StrainedNetworkSurvey.prototype.backButton_;

esn.StrainedNetworkSurvey.prototype.createDom = function() {
  goog.base(this, 'createDom');

  this.setElementInternal(
      soy.renderAsElement(esn.soy.strainedNetworkSurvey.content));

  this.autocomplete_ = new goog.ui.AutoComplete.Basic(this.students_,
      this.getElementByClass('friendAutocompleteInput'), false);
  var label = new goog.ui.LabelInput();
  label.decorate(this.getElementByClass('friendAutocompleteInput'));

  this.strainedFriendSurveryList_ = new esn.StrainedFriendSurveyList(
      this.appContext_, this.getDomHelper());
  this.strainedFriendSurveryList_.render(
      this.getElementByClass('friendListContainer'));

  this.surveyClient_.getStrainedFriendSurveys(
      goog.bind(this.getStrainedFriendSurveysResponse_, this));

  this.continueButton_ = new goog.ui.CustomButton(
      esn.constants.getContinueButtonCaption());
  this.continueButton_.render(this.getElementByClass('continueButton'))

  this.backButton_ = new goog.ui.CustomButton(
      esn.constants.getBackButtonCaption());
  this.backButton_.render(this.getElementByClass('backButton'));
};

esn.StrainedNetworkSurvey.prototype.enterDocument = function() {
  goog.base(this, 'enterDocument');

  this.getHandler().listen(
      this.autocomplete_,
      goog.ui.AutoComplete.EventType.UPDATE,
      this.handleAutocompleteSelect_);

  this.getHandler().listen(
      this.continueButton_,
      goog.ui.Component.EventType.ACTION,
      this.handleContinueClick_);

  this.getHandler().listen(
      this.backButton_,
      goog.ui.Component.EventType.ACTION,
      this.handleBackClick_);
};

esn.StrainedNetworkSurvey.prototype.getStrainedFriendSurveysResponse_ =
    function(response) {
  if (!response.hasError()) {
    var strainedFriendSurveys = response.getData();
    if (strainedFriendSurveys.length > 0) {
      goog.style.showElement(this.getElementByClass('friendListContainer'),
          true);
      this.strainedFriendSurveryList_.addFriends(strainedFriendSurveys);
    }
  }
};

esn.StrainedNetworkSurvey.prototype.handleAutocompleteSelect_ = function(e) {
    goog.style.showElement(this.getElementByClass('friendListContainer'),
        true);
  this.strainedFriendSurveryList_.addFriend(e.row, true);
  this.getElementByClass('friendAutocompleteInput').value = '';
};

esn.StrainedNetworkSurvey.prototype.handleContinueClick_ = function(e) {
  if (this.strainedFriendSurveryList_.validateCompletion()) {
    this.dispatchChangeEvent_();
    this.dispatchEvent(esn.StrainedNetworkSurvey.EventType.CONTINUE);
  }
};

esn.StrainedNetworkSurvey.prototype.handleBackClick_ = function(e) {
  this.dispatchEvent(esn.StrainedNetworkSurvey.EventType.BACK);
};

/**
 * Dispatches change event to update progress bar.
 */
esn.StrainedNetworkSurvey.prototype.dispatchChangeEvent_ = function() {
  this.dispatchEvent(new esn.BaseSurvey.ChangeEvent(
      esn.StrainedNetworkSurvey.SECTION_ID, 1, 1));
};
